package com.seed.application.service;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.row.Row;
import com.seed.application.Sa;
import com.seed.application.service.detail.DetailService;
import com.seed.application.service.get.GetService;
import com.seed.application.service.page.PageService;
import com.seed.application.service.pojo.SeedAction;
import com.seed.application.service.remove.RemoveService;
import com.seed.application.service.save.SaveService;
import com.seed.application.service.set.SetService;
import com.seed.application.service.stat.StatService;
import com.seed.application.service.update.UpdateService;
import com.seed.core.action.*;
import com.seed.core.pojo.GetVO;
import com.seed.core.pojo.SetDTO;
import com.seed.core.pojo.StatPage;
import com.seed.core.pojo.seed.Seed;
import com.seed.core.query.*;
import com.seed.application.web.WebContext;
import com.seed.util.SeedKit;
import com.seed.util.ThrowKit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Service
public class SeedServiceImpl {

//    private final WebContext web;
//
//    public SeedServiceImpl(WebContext web) {
//        this.web = web;
//    }
//
//    public StatPage<Row> page(Integer pageNumber, Integer pageSize) {
//        if(pageNumber == null) pageNumber = PageService.PAGE_NUMBER;
//        if(pageSize == null) pageSize = PageService.PAGE_SIZE;
//
//        SeedAction seedAction = getSeedAction(RequestMethod.GET);
//        Seed seed = seedAction.getSeed();
//        Action action = seedAction.getAction();
//
//        return new PageService(seed, (PageAction) action)
//                .pageNumber(pageNumber)
//                .pageSize(pageSize)
//                .searchParams(web.getSearch())
//                .orderParams(web.getOrder())
//                .keyword(web.getKeyword())
//                .dataPermis(web.getDataPermis())
//                .fieldPermits(web.getFieldPermis())
//                .exec();
//    }
//
//    @Transactional
//    public boolean save(Row row) {
//        SeedAction seedAction = getSeedAction(RequestMethod.GET);
//        Seed seed = seedAction.getSeed();
//        Action action = seedAction.getAction();
//        return new SaveService(seed, (SaveAction) action).exec(row);
//    }
//
//    @Transactional
//    public boolean saveBatch(List<Row> dataList) {
//        SeedAction seedAction = getSeedAction(RequestMethod.GET);
//        Seed seed = seedAction.getSeed();
//        Action action = seedAction.getAction();
//        return new SaveService(seed, (SaveAction) action).execBatch(dataList);
//    }
//
//    @Transactional
//    public boolean update(Row row) {
//        SeedAction seedAction = getSeedAction(RequestMethod.GET);
//        Seed seed = seedAction.getSeed();
//        Action action = seedAction.getAction();
//        return new UpdateService(seed, (UpdateAction) action).exec(row);
//    }
//
//    @Override
//    public boolean updateBatch(List<Row> rows) {
//        SeedAction seedAction = getSeedAction(RequestMethod.GET);
//        Seed seed = seedAction.getSeed();
//        Action action = seedAction.getAction();
//        return new UpdateService(seed, (UpdateAction) action).execBatch(rows);
//    }
//
//    public Row detail(Serializable id) {
//        SeedAction seedAction = getSeedAction(RequestMethod.GET);
//        Seed seed = seedAction.getSeed();
//        Action action = seedAction.getAction();
//
//        return new DetailService(seed, (DetailAction) action)
//                .dataPermis(web.getDataPermis())
//                .fieldPermits(web.getFieldPermis())
//                .exec(id);
//    }
//
//    @Transactional
//    public boolean remove(Serializable id) {
//        SeedAction seedAction = getSeedAction(RequestMethod.DELETE);
//        Seed seed = seedAction.getSeed();
//        Action action = seedAction.getAction();
//        return new RemoveService(seed, (RemoveAction) action).exec(id);
//    }
//
//    @Override
//    public boolean removeBatch(List<Serializable> ids) {
//        SeedAction seedAction = getSeedAction(RequestMethod.DELETE);
//        Seed seed = seedAction.getSeed();
//        Action action = seedAction.getAction();
//        return new RemoveService(seed, (RemoveAction) action).execBatch(ids);
//    }
//
//    public GetVO get(Serializable id) {
//        SeedAction seedAction = getSeedAction(RequestMethod.GET);
//        Seed seed = seedAction.getSeed();
//        Action action = seedAction.getAction();
//        return new GetService(seed, (GetAction) action).exec(id);
//    }
//
//    public boolean set(SetDTO dto) {
//        SeedAction seedAction = getSeedAction(RequestMethod.PUT);
//        Seed seed = seedAction.getSeed();
//        Action action = seedAction.getAction();
//        return new SetService(seed, (SetAction) action).exec(dto);
//    }
//
//    public Map<String, Object> stat() {
//        SeedAction seedAction = getSeedAction(RequestMethod.GET);
//        Seed seed = seedAction.getSeed();
//        Action action = seedAction.getAction();
//
//        return new StatService(seed, (StatAction) action)
//                .searchParams(web.getSearch())
//                .dataPermis(web.getDataPermis())
//                .keyword(web.getKeyword())
//                .exec();
//    }
//
//    private SeedAction getSeedAction(RequestMethod method) {
//        String seedName = web.getSeedName();
//        String actionName = web.getActionName();
//        Micro micro = Sa.getMicro();
//        Seed seed;
//        Action action;
//        if(StrUtil.isBlank(seedName)) {
//            Map<String, Action> actions = micro.getActions();
//            if(actions == null) ThrowKit.exception("请求的方法【%s】不存在", actionName);
//
//            action = actions.get(actionName);
//            if(action == null || action.getMethods() == null || !(action.getMethods()).contains(method)) {
//                ThrowKit.exception("请求的方法【%s】不存在", actionName);
//            }
//
//            seedName = action.getSeedName();
//            if(StrUtil.isBlank(seedName)) ThrowKit.exception("【%s】必需配置【seedName】", actionName);
//            seed = micro.getSeed(seedName);
//        } else {
//            seed = micro.getSeed(seedName);
//            action = SeedKit.getAction(seed, method, actionName);
//        }
//
//        return new SeedAction(seed, action);
//    }
}
